from django.shortcuts import render
from django.shortcuts import redirect
from django.shortcuts import HttpResponse
from django.views import View
from django.http import JsonResponse
from web.forms.project import ProjectModelForm
from web import models
from utils.tencent.cos import creat_bucket
import time
class ProjectList(View):
    '''
    后台管理 项目列表视图
    '''
    def get(self,request):
        project_dict={'star':[],'join':[],'my':[]}
        my_project_queryset=models.Project.objects.filter(creator=request.tracer.user)
        for item in my_project_queryset:
            if item.star:
                project_dict['star'].append({"value": item, 'type': 'my'})
            else:
                project_dict['my'].append(item)
        projectuser_queryset=models.ProjectUser.objects.filter(user=request.tracer.user)
        for item in projectuser_queryset:
            if item.star:
                project_dict['start'].append({"value": item.project, 'type': 'join'})
            else:
                project_dict['join'].append(item.project)

        form=ProjectModelForm(request)
        return render(request,'web/project_list.html',{'form':form,'project_dict':project_dict})

    def post(self,request):

        form = ProjectModelForm(request,data=request.POST)
        if form.is_valid():
            #在项目创建的时候 为项目创建一个存储桶
            name=form.cleaned_data.get('name')
            bukcet='{}-{}-1307211509'.format(request.tracer.user.mobile_phone,str(int(time.time())))
            region='ap-nanjing'
            try:
                creat_bucket(bukcet,region)
            except Exception as e:
                print('创建失败')
                return HttpResponse('给项目分配存储桶失败')
            #把桶和区域写入Prokect数据库
            form.instance.bucket=bukcet
            form.instance.region=region
            form.instance.creator=request.tracer.user
            instance=form.save()
            #项目初始化的时候创建问题类型
            issue_type_list=[]
            for item in models.IssuesType.PROJECT_INIT_LIST:
                issue_type_list.append(models.IssuesType(project=instance,title=item))
            models.IssuesType.objects.bulk_create(issue_type_list)
            return JsonResponse({'status':True,})
        else:
            print(form.errors)
            return JsonResponse({'status':False,'error':form.errors,})


class Project_Star(View):
    '''
    项目的标星视图处理
    '''
    def get(self,request,project_type,project_id):
        if project_type=='my':
            models.Project.objects.filter(pk=project_id,creator=request.tracer.user).update(star=True)
            return redirect('web:project_list')
        if project_type=='join':
            models.ProjectUser.objects.filter(project_id=project_id,user=request.tracer.user).update(star=True)
            return redirect('web:project_list')
        return HttpResponse('请求错误')

class Project_UnStar(View):
    '''
    去除星标
    '''
    def get(self,request,project_type,project_id):
        if project_type=='my':
            models.Project.objects.filter(pk=project_id,creator=request.tracer.user).update(star=False)
            return redirect('web:project_list')
        if project_type=='join':
            models.ProjectUser.objects.filter(project_id=project_id,user=request.tracer.user).update(star=False)
            return redirect('web:project_list')
        return HttpResponse('请求错误')